package com.example.ovulationperiodtracker.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Space;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.example.ovulationperiodtracker.R;
import com.example.ovulationperiodtracker.databinding.ViewNavigationBinding;
import com.gyf.immersionbar.ImmersionBar;

public class NavigationView extends FrameLayout {

    private ViewNavigationBinding binding;
    private OnClickTitleListener onClickTitleListener;
    private OnClickRightListener onClickRightListener;

    public NavigationView(Context context) {
        this(context, null);
    }

    public NavigationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        binding = ViewNavigationBinding.inflate(LayoutInflater.from(context), this, true);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NavigationView);
        boolean back = ta.getBoolean(R.styleable.NavigationView_back, false);
        String title = ta.getString(R.styleable.NavigationView_title);
        String rightText = ta.getString(R.styleable.NavigationView_rightText);
        int titleColor = ta.getColor(R.styleable.NavigationView_titleColor,
                ContextCompat.getColor(context, R.color.black));
        int background = ta.getResourceId(R.styleable.NavigationView_background,
                R.color.transparent);
        int backImage = ta.getResourceId(R.styleable.NavigationView_backImage,
                R.mipmap.ic_back);
        ta.recycle();

        setBackgroundResource(background);
        binding.tvTitle.setText(title);
        binding.tvTitle.setTextColor(titleColor);
        binding.ivBack.setVisibility(back ? VISIBLE : GONE);
        binding.ivBack.setImageResource(backImage);
        if (context instanceof AppCompatActivity) {
            AppCompatActivity activity = (AppCompatActivity) context;
            int height = ImmersionBar.getStatusBarHeight(activity);
            Space space = findViewById(R.id.space);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) space.getLayoutParams();
            params.height = height;
            space.setLayoutParams(params);
        }

        if (rightText != null) {
            binding.tvRight.setVisibility(View.VISIBLE);
            binding.tvRight.setText(rightText);
            binding.tvRight.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickRightListener != null) {
                        onClickRightListener.onClickRight();
                    }
                }
            });
        }

        binding.ivBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (context instanceof AppCompatActivity) {
                    ((AppCompatActivity) context).finish();
                }
            }
        });

        binding.tvTitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickTitleListener != null) {
                    onClickTitleListener.onClickTitle();
                }
            }
        });
    }

    public void setTitle(String title) {
        binding.tvTitle.setText(title);
    }

    public void setOnClickTitleListener(OnClickTitleListener listener) {
        this.onClickTitleListener = listener;
    }
    public void setOnClickRightListener(OnClickRightListener listener) {
        this.onClickRightListener = listener;
    }

    public interface OnClickTitleListener {
        void onClickTitle();
    }
    public interface OnClickRightListener {
        void onClickRight();
    }
}